<?php
//session_start();
//header("Content-type:text/html;charset=utf-8");
require_once 'Medoo.php';
require_once 'Contact.php';
//禁用错误报告
ini_set("display_errors", 0);
error_reporting(E_ALL ^ E_NOTICE);
error_reporting(E_ALL ^ E_WARNING);
/**
 * 后台管理登陆类 POST
 * 前端入参：
 *  username:  用户名
 *  psd:  密码
 */

$rws_post = file_get_contents('php://input');
$mypost = json_decode($rws_post);
$name = (string)$mypost->username;
$pwd = (string)$mypost->pwd;

if ($name == null || $pwd == null) {
    returnResult(-2);
    return;
}

select();

/**
 * 查询
 */
function select()
{
    global $database;
    global $name;
    global $pwd;

    if (!hasRecoved()) {
        returnResult(-1);
        return;
    }

    // 查询数据示例
    $datas = $database->select("manage_user", [
        "password"
    ], [
        "user_name[=]" => $name
    ]);

    foreach ($datas as $data) {
        $result = urlencode($data["password"]);
        if ($result == md5($pwd)) {
            returnResult(1);
        } else {
            returnResult(0);
        }
    }
}


/**
 * @return 判断是否有该条记录
 */
function hasRecoved()
{
    global $database;
    global $modularID;
    global $name;
    if ($database->has("manage_user", array("user_name" => $name))) {
        return true;
    } else {
        return false;
    }
}


/**
 * @param $resultType 返回结果
 */
function returnResult($resultType)
{
    $resultMsg = "";
    $resultCode = "";
    if ($resultType == 1) {
        $resultCode = 200;
        $resultMsg = '登陆成功';
    } else if ($resultType == -1) {
        $resultCode = -1;
        $resultMsg = '无此账户';
    } else {
        $resultCode = 1;
        $resultMsg = '密码有误';
    }
    if ($resultType == -2) {
        $resultCode = -2;
        $resultMsg = '错误访问';
    }
    $result = array(
        'code' => urlencode($resultCode),
        'msg' => urlencode($resultMsg),
        'data' => null
    );

    echo urldecode(json_encode($result));
}
